for each tuple tr in r do begin
for each tuple ts in s do begin
test pair (tr, ts) to see if they satisfy the join condition
if they do, add tr·ts to the result
end
end
最坏情况(只能载入两个 block,r/s 各一个)
block transfers:
r 共要载入 次,而 s 共要载入 次
seek times:
r 共要 seek 次,而 s 共要 seek 次
最好情况(全部元组都能直接载入内存)
block transfers
seek times:
(2) Block Nested-Loop Join
for each block Br in r do begin
for each block Bs in s do begin
for each tuple tr in Br do begin
for each tuple ts in Bs do begin
test pair (tr, ts) to see if they satisfy the join condition
if they do, add tr·ts to the result
end
end
end
end
最坏情况(只能载入两个 block,r/s 各一个)
block transfers:
r 共要载入 次,而 s 共要载入 次
seek times:
r 共要 seek 次,而 s 共要 seek 次
最好情况(全部元组都能直接载入内存)
block transfers
seek times:
(3) Indexed Nested-Loop Join
原理:外层遍历 tuple(每次读取一个 block),内层使用索引匹配(如 B+ 树索引等)
最坏情况(只能载入两个 block,r/s 各一个)
因为索引本身的复杂度较复杂,故只给出较粗略的估算
其中 指单次查询的平均开销
(4) Merge-Join
算法流程
根据 index 的 attributes 进行排序
连接两个排序过的表(从头到尾扫描)
理想情况下(每个块只需要读进内存一次)
block transfers: +(排序)
seek times: +(排序)
(5) Hash-Join
大致流程如下
将r划分成nh个块
将s划分成nh个块
for 0 to nh-1
对r对应块中的元素建立索引Ri
for 遍历s对应块中的元素
检索索引Ri
for 所有匹配的tuple in r
join
end
end
end
Conjunctive selection operations can be deconstructed into a sequence of indicidual selections
Selection operations are commutative
Only the last in a sequence of projection operations is needed, the others can be ommitted.
Selections can be combined with Cartesian products and theta joins.
(1)
(2)
Theta-join operations (and natural joins) are commutative.
Natural join operatins are associative , Theta joins are associative in the following manner , where involves attributes from only and .
The selection operation distributes over the theta join operation under the following two conditions:
(1) When all the attributes in involve only the attributes of one of the expressions () being joined
(2) When involves only the attributes of and involves only the attributes of .
The projetion operation distributes over the theta join operation as follows:
(1) if involves only attributes from :
(2) Consider a join .
(3) Let and be sets of attributes from and respectively
(4) Let be attributes of that are involved in join condition , but are not in
(5) Let be attributes of that are involved in join condition , but are not in
The set operations union and intersection are commutative ,
Set union and intersection are assocative ,
The selection operation distributes over , and
The projection operation distributes over union
Cost Estimation(结果集大小估计)
基本概念
: number of tuples in a relation
: number of blocks containing tuples of
: size of a tuple of
: blocking factor of
: number of distinct values that appear in for attribute ; same as the size of